package com.capricornus.demo.config;

import com.capricornus.demo.common.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ApplicationExceptionHandler {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResponseResult<Object> exception(HttpServletRequest request, Exception ex) {
		ex.printStackTrace();
		String msg = "异常:" + ex.getClass() + "||方法:" + ex.getStackTrace()[0].getMethodName() + "||类名:" + ex.getStackTrace()[0].getClassName() + "||行数:" + ex.getStackTrace()[0].getLineNumber();
		logger.error(msg);
		return ResponseResult.error(ex.getMessage());
	}
}
